
package com.wisevar.thirdplatform.gateway.aspect;

import cn.hutool.core.collection.CollUtil;
import com.wisevar.thirdplatform.gateway.exception.BaseException;
import com.wisevar.thirdplatform.gateway.model.common.ResultCode;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Set;

@Order(0)
@Aspect
@Component
public class OpenApiWhiteIpAspect {


    @Pointcut("@annotation(com.wisevar.thirdplatform.gateway.aspect.OpenApiWhiteIp)")
    private void openApiWhiteIpPointCut() {
    }


    @Before("openApiWhiteIpPointCut() && @annotation(openApiWhiteIp)")
    public void doBefore(OpenApiWhiteIp openApiWhiteIp) throws Throwable {
        Set<String> whiteIpSet = CollUtil.emptyIfNull(getWhiteIpSet());
        String currentIp = getCurrentIp();
        if (!whiteIpSet.contains(currentIp)){
            throw new BaseException(ResultCode.IP_NOT_FOUND);
        }
    }


    private Set<String> getWhiteIpSet() {
        // todo
        return null;
    }

    private String getCurrentIp() {
        // todo
        return "";
    }


}
